package ytt.spec.edu.weekhome02;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class update_psd_Activity extends LoginActivity {
  private static final String TAG="update_psd";
    private AutoCompleteTextView nEmailView;

    private EditText nPasswordView;

    private EditText nPasswordView2;

    private String success2;

    private String date2;

    private String total2;

    private String message2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_psd_);
        Button button=findViewById(R.id.button_2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            nEmailView=(AutoCompleteTextView) findViewById(R.id.num2);
                String studentNumber = nEmailView.getText().toString();

                sendRequestWihtOkhttp2();
                if(success2 == "true")

                {

                    nPasswordView = (EditText) findViewById(R.id.ps2);

                    nPasswordView2 = (EditText) findViewById(R.id.ps3);

                    String psd2=nPasswordView.getText().toString();

                    String psd3=nPasswordView2.getText().toString();

                    if(psd2.equals(psd3)==true){

                        Intent intent=new Intent(update_psd_Activity.this,LoginActivity.class);

                        Toast.makeText(update_psd_Activity.this," "+success2+",data:"+date2+",total:"+total2+message2,Toast.LENGTH_SHORT).show();

                        //password=psd2;

                        startActivity(intent);

                    }else if(psd2.equals(psd3)==false)

                    {

                        Toast.makeText(update_psd_Activity.this,"修改失败",Toast.LENGTH_SHORT).show();

                        nEmailView.getText().clear();

                        nPasswordView.getText().clear();

                        nPasswordView2.getText().clear();

                    }

                }else if(success2 == "false")

                {

                    Toast.makeText(update_psd_Activity.this,"用户名错误",Toast.LENGTH_SHORT).show();

                }
            }
        });

    }
    private void sendRequestWihtOkhttp2() {

        new Thread(new Runnable(){

            private String studentNumber;

            @Override

            public void run() {
            try{
                OkHttpClient client = new OkHttpClient();

                Request request = new Request

                        .Builder()

                        .url("http://www.yyjsx.xyz/api/student/updatePassword"+"?studentNumber="+studentNumber+"&password=")

                        .build();
                Response response = client.newCall(request).execute();

                final String body2 = response.body().string();

                Log.d(TAG,body2);

                parseJson(body2);

            }catch (Exception e){
                e.printStackTrace();

            }

            }
            }).start();

            }
    private void parseJson(String jsonStr) {

        try {

            JSONObject jobj = new JSONObject(jsonStr);

            success2 = jobj.getString("success");

            date2 = jobj.getString("data");

            total2 = jobj.getString("total");

            message2 = jobj.getString("message");

            Log.d(TAG,success2);

        } catch (JSONException e) {

            e.printStackTrace();

        }

    }

}
